<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="demoTable layui-form">

            投诉类型：
            <div class="layui-inline">
                <select name="comId"  id="typeId" lay-verify="required" lay-search="">
                    <option value="">选择投诉类型</option>
                </select>
            </div>
            处理状态：
            <div class="layui-inline">
                <select name="status"  id="status" lay-verify="required" >
                    <option value="">选择状态</option>
                    <option value="0">待处理</option>
                    <option value="1">已处理</option>
                </select>
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
        </div>

        <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            {{# if(d.status=='0'){ }}
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">投诉处理</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            {{# }else{ }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            {{# } }}
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;



        table.render({
            elem: '#currentTableId',
            url: 'complaint/queryComplaintAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                // {field: 'id', width: 80, title: 'ID', sort: true},
                {templet:'<div>{{d.type.name}}</div>',width: 100, title: '投诉类型'},
                {field: 'remarks', width: 120, title: '投诉内容'},
                {templet:'<div>{{d.owner.username}}</div>',width: 100, title: '投诉人'},
                {field: 'comDate', width: 120, title: '投诉时间'},
                {field: 'status', width: 80, title: '处理状态',
                    templet: function(res){
                        if(res.status=='0'){
                            return '<span class="layui-badge layui-bg-red" >待处理</span>';
                        }else{
                            return '<span class="layui-badge layui-bg-gray" >已处理</span>';
                        }
                    }
                },
                {field: 'clr', width: 100, title: '处理人'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            id: 'testReload'
        });


        //获取费用类型 并渲染数据到select标签
        $.get("complainttype/queryAll",{},function (data) {
            var list=data;
            var select =document.getElementById("typeId");
            if(list!=null || list.size()>0){
                for(var c in list){
                    var option=document.createElement("option");
                    option.setAttribute("value",list[c].id);
                    option.innerText=list[c].name;
                    select.appendChild(option);
                }
            }
            form.render('select');
        },"json");


        var $ = layui.$, active = {
            reload: function(){
                var comId = $('#typeId');
                var status = $('#status');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        status:status.val(),
                        comId:comId.val()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
           if (obj.event === 'delete') {  // 监听删除操作
                /*
                  1、判断是否有选择记录 如果没有提示
                  2、获取要删除的id集合
                  3、发送请求 实现删除功能
                  4、提示成功 并刷新主页
                 */
                var checkStatus=table.checkStatus(obj.config.id);
                var data =checkStatus.data;
                if(data.length==0){
                    layer.msg("请选择要删除的记录信息");
                }else{
                    //获取要删除id的集合
                    var ids =getCheckId(data);
                    layer.confirm('你确定要删除记录吗？',function (index) {
                         //执行删除功能
                        deleteByIds($,"complaint/deleteByIds",ids,index);
                    })

                }

            }
        });



        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.confirm('现在处理么？', function (index) {
                    $.ajax({
                        url:"complaint/update",
                        type:"post",
                        data:{'id':data.id},
                        success:function (result) {
                            if(result.code==200){
                                layer.msg('成功处理',{
                                    iocn:6,
                                    time:500
                                },function () {
                                    //重载父窗口 关闭当前窗口
                                    parent.window.location.reload();
                                    var iframeIndex=parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(iframeIndex);
                                })
                            }else{
                                layer.msg("处理失败");
                            }
                        }
                    })
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    deleteByIds($,"complaint/deleteByIds",data.id,index)
                });
            }
        });

    });
</script>